home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!news
- From: watzka@stat.uni-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: How to access memory allocated in function
- Date: 21 Apr 1996 15:55:09 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4ldlot$9uv@sparcserver.lrz-muenchen.de>
- References: <4ddbe3$so3@josie.abo.fi> <slrn4nkrsq.bv6.shadows@whateva.kuwait.net>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
- shadows@whateva.kuwait.net (The ShadowS Know) writes:
-
- >In article <4ddbe3$so3@josie.abo.fi>, Christoffer Sundqvist wrote:
- >>How can i access memory allocated dynamically in a function after i leave the
- >>function, something like this:
- >>
- >>void sub(int *p)
- >>{
- >> p = malloc(.....);
- >>}
- >>
-
- >p = (int *)malloc(sizeof(int));
- >What we've done is cast it as an pointer to an integer then allocated it
- >the size of one int (thus it points to a space in memory that has the size
- >of holding 1 integer). Ofcourse anything might be in that memory address
- >you'd need to place something there to clear it up.
-
- As a solution to the original posters problem, this is entirely useless,
- but it is not completely useless, since it can still be used as a bad
- example :-).
-
- The problem that pointers are passed by value like all other variables in
- C is treated in the FAQ for comp.lang.c in the question named
- "I have a function which accepts, and is supposed to initialize, a pointer"
- using somewhat archaic syntax. For those of us who cannot decipher
- K&R C, Steve Summit describes:
-
- void f(int *ip)
- {
- satic int dummy = 5;
- ip = &dummy;
- }
-
- Casting the return value of malloc() is not a useful tip for standard
- C, but it may be needed for older implementations. An implementation
- that accepts "void sub(int *p)" but declares malloc() as returning
- something other than a "void *" in <stdlib.h> might be a rare
- collectors item.
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
-
-
-